<?php


namespace App\Http\Controllers\android\service;


use App\Http\Controllers\admin\model\TfMediaModel;
use App\Http\Controllers\android\model\BrowserModel;
use App\Http\Controllers\android\model\LabelModel;
use App\Http\Controllers\android\model\ParentSnModel;
use App\Http\Controllers\android\model\SettingModel;
use App\Http\Controllers\android\model\TeacherSnModel;
use App\Http\Controllers\android\model\TFMediaTypeModel;
use App\Http\Controllers\utils\ToolUtil;
use Illuminate\Support\Facades\DB;


class ControlService
{
    /**
     * 获取系统设置其它管控
     * @param $param
     * @return false|string
     */
    public static function getSetting($param){
        $parentSn = ParentSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['a.is_lock','p.id','teacher_id']);
        if(!$parentSn){
            return ToolUtil::returnJson(400,'没有该设备');
        }
        if($parentSn['is_lock'] === 0){
            return ToolUtil::returnJson(400,'没有数据');
        }
        $field = ['setting_white_app','setting_call','setting_memory','setting_usb','setting_bluetooth','setting_navigation','setting_statusbar',
            'setting_tfmedia','setting_phones','setting_phone','setting_camera','setting_tfmedia_format','setting_clock',
            'setting_recording','setting_music','setting_picture','setting_wallpaper','setting_file','setting_bhtvideo',
            'setting_context','setting_bht','setting_hotspot','qch_restore','setting_browserInput','dev_mode','search_topic','setting_browser','is_eyecare','eyecare_near','eyecare_lie','eyecare_shake','eyecare_light','eyecare_bw','eyecare_tired','eyecare_tired_val'];
        if(empty($parentSn['teacher_id'])){
            $setting = SettingModel::queryFirst(['sn_value'=>$param['sn']],$field);
            if($setting){
                $data = self::handleSetting($setting);
                return ToolUtil::returnJson(200,'成功',$data);
            }
        }else{
            $teacher = TeacherSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['b.id']);
            if($teacher){
                $setting = SettingModel::queryLikeFirst(['field'=>'batch_ids','batch_ids'=>$teacher['id']],$field);
                if($setting){
                    $data = self::handleSetting($setting);
                    return ToolUtil::returnJson(200,'成功',$data);
                }
            }
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理tf卡设置
     * @param $param
     * @return mixed
     */
    private static function handleSetting($param){
        if(empty($param['setting_phones'])){
            $param['setting_phones'] = "";
        }
        if(empty($param['setting_context'])){
            $param['setting_context'] = "";
        }
        if($param['setting_tfmedia_format']){
            $tfArr = explode(',',$param['setting_tfmedia_format']);
            $arr = [];
            foreach ($tfArr as $k=>$v){
                $arr[$k]=TfMediaModel::queryValue(['id'=>$v],'format');
            }
            $param['setting_tfmedia_format'] = implode(',',$arr);
        }else{
            $param['setting_tfmedia_format'] = "";
        }
        return $param;
    }

    /**
     * 获取浏览器网址信息
     * @param $param
     * @return false|string
     */
    public static function getBrowser($param){
        $parentSn = ParentSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['p.id','teacher_id']);
        if(!$parentSn){
            return ToolUtil::returnJson(400,'没有该设备');
        }
        $field = ['address','type'];
        if($parentSn['teacher_id']){
            $teacher = TeacherSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['b.id']);
            if($teacher){
                $batch = $teacher['id'];
                $browser = DB::table('t_browser')
                    ->whereRaw(" find_in_set('{$batch}',batch_ids) ")
                    ->whereRaw(" ( type = 0 or type = 1 ) ")
                    ->get($field);
                if(count($browser)){
                    $data = self::handleBrowser($browser);
                    return ToolUtil::returnJson(200,'成功',$data);
                }
            }
        }else{
            $browser = DB::table('t_browser')
                ->where('sn_value',$param['sn'])
                ->whereRaw(" ( type = 0 or type = 1 ) ")
                ->get($field);
            if(count($browser)){
                $data = self::handleBrowser($browser);
                return ToolUtil::returnJson(200,'成功',$data);
            }
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理浏览器上网信息
     * @param $param
     * @return mixed
     */
    private static function handleBrowser($param){
        $browser = [];
        $browser_black = [];
        foreach ($param as $k=>$v){
            if($v['type']){
                $browser[$k] = $v;
            }else{
                $browser_black[$k] = $v;
            }
        }
        $data['browser'] = array_values($browser);
        $data['browser_black'] = array_values($browser_black);
        return $data;
    }

    /**
     * 获取浏览器书签信息
     * @param $param
     * @return false|string
     */
    public static function getLabel($param){

        $parentSn = ParentSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['p.id','teacher_id']);
        if(!$parentSn){
            return ToolUtil::returnJson(400,'没有该设备');
        }

        if($parentSn['teacher_id']){
            $teacher = TeacherSnModel::queryLeftAdminSn(['a.value'=>$param['sn']],['b.id']);
            if($teacher){
                $batch = $teacher['id'];
                $label = DB::table('t_browser')
                    ->whereRaw(" find_in_set('{$batch}',batch_ids) ")
                    ->whereRaw(" ( type = 2 or type = 3 ) ")
                    ->get(['address','type']);
                if(count($label)){

                    $data = self::handleLabel($label);
                    return ToolUtil::returnJson(200,'成功',$data);

                }
            }
        }else{
            $label = DB::table('t_browser')
                ->where('sn_value',$param['sn'])
                ->whereRaw(" ( type = 2 or type = 3 ) ")
                ->get(['address','type']);
            if(count($label)){

                $data = self::handleLabel($label);
                return ToolUtil::returnJson(200,'成功',$data);

            }
        }

        return ToolUtil::returnJson(400,'没有数据');

    }

    private static function handleLabel($param){

        $home_page = '';
        $label_page = '';
        foreach ($param as $key => $value) {

            if($value['type'] === 2){
                $home_page = $value['address'];
            }else if($value['type'] === 3){
                $label_page .= $value['address'].',';
            }
            
        }

        $data['home_page'] = $home_page;
        $data['label_page'] = rtrim($label_page,',');

        return $data;
    }



    /**
     * sn获取应用ID管控
     * @param $param
     * @return false|string
     */
    public static function queryAppInside($param){

        $Inside = DB::table('t_app_inside')->where('sn',$param['sn'])->get(['package','content']);
        if(count($Inside)){

            $content = '';
            $package = '';
            foreach ($Inside as $key => $value) {
                $c = trim($value['content']);
                if(!empty($c)){
                    $content .= $value['content'].',';
                }
                $p = trim($value['package']);
                if(!empty($p)){
                    $package .= $value['package'].',';
                }
                
            }

            $data['ids'] = rtrim($content,',');
            $data['package'] = rtrim($package,',');

            return ToolUtil::returnJson(200,'成功',$data);
        }

        return ToolUtil::returnJson(400,'没有数据');

    }


}